package com.yykj.user.center.server.repository.patient;

import com.yykj.user.center.server.model.ConsultationInfoOrder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chenl
 * @version 1.0
 * @date 2021/10/20 22:24
 */
@Repository
public interface ConsultationInfoRepository extends JpaRepository<ConsultationInfoOrder, String>, JpaSpecificationExecutor<ConsultationInfoOrder> {
    ConsultationInfoOrder findFirstByPayAccountIdAndOrderNo(String accountId, String orderNo);

    ConsultationInfoOrder findFirstByOrderNo(String orderNo);

    List<ConsultationInfoOrder> findAllByPayAccountIdAndStateOrderByCreateTimeDesc(String accountId, Integer state);

    Integer countAllByStateInAndDoctorId(int[] states, String doctorId);

    List<ConsultationInfoOrder> findAllByStateInAndDoctorId(int[] states, String doctorId);

    List<ConsultationInfoOrder> findAllByState(Integer state);


    List<ConsultationInfoOrder> findAllByDoctorIdAndOrderTypeInAndPayStatus(String doctorId, int[] orderTypes, Integer status);

    List<ConsultationInfoOrder> findAllByPatientCardIdInAndState(List<String> patientCardIds, Integer state);


    List<ConsultationInfoOrder> findAllByDoctorIdAndState(String doctorId, Integer state);

    List<ConsultationInfoOrder> findAllByPatientCardIdNotInAndState(List<String> patientCardIds, Integer state);

    Integer countAllByDoctorId(String doctorId);

    Integer countAllByDateAndDoctorIdAndStateIn(LocalDate localDate, String doctorId, int[] states);

    Integer countAllByDoctorIdAndState(String doctorId, Integer state);

    ConsultationInfoOrder findFirstByPayAccountIdOrderByPayTimeDesc(String accountId);

    ConsultationInfoOrder findFirstByDoctorIdAndPayAccountIdOrderByCreateTimeDesc(String doctorId, String userId);

    ConsultationInfoOrder findFirstByPatientCardIdAndPayStatusAndRefundStatusAndStartDatetimeAndEndDatetimeAndStateInOrderByCreateTimeDesc(String cardId, Integer payStatus, Integer refund, LocalDateTime start, LocalDateTime end, Integer[] state);

    @Query("SELECT t from ConsultationInfoOrder t \n" +
            "where t.payAccountId = ?1 \n" +
            "and t.state  <> ?2 and t.payStatus  = ?3 \n" +
            "group by t.doctorId order by t.createTime desc")
    List<ConsultationInfoOrder> findAllByPayAccountIdAndStateGroupByDoctorIdOrderByCreateTimeDesc(String payAccountId, Integer state, Integer payStatus);

    @Query("SELECT t from ConsultationInfoOrder t \n" +
            "where t.patientCardId in (?1) \n" +
            "and t.state in (?2) \n" +
            "group by t.patientCardId order by t.createTime desc")
    List<ConsultationInfoOrder> findAllByPatientCardIdInAndStateIn(List<String> patientCardIds, List<Integer> state);


    @Query("SELECT doctorId,count(*) as count from ConsultationInfoOrder t \n" +
            "where t.doctorId in (?1) \n" +
            "group by t.doctorId")
    List countByDoctorIdInGroup(List<String> doctorIds);

    @Query(value = "SELECT doctor_id,AVG(diff) as diff from ( \n" +
            "SELECT doctor_id,TIMESTAMPDIFF(MINUTE, CONCAT(date,' 10:00:00'), receive_time) as diff from core_consultation_info_order where receive_time is not null and doctor_id in (?1) " +
            ") a where a.diff > 0 group by doctor_id", nativeQuery = true)
    List avgReceiveTimeByDoctorGroup(List<String> doctorIds);

    List<ConsultationInfoOrder> findByPayAccountIdOrderByCreateTimeDesc(String userId);

    ConsultationInfoOrder findFirstByPrescriptionId(String prescriptionId);

    List<ConsultationInfoOrder> findByPatientCardIdOrderByCreateTimeDesc(String userId);

    Integer countAllByReceiveTimeBetweenAndDoctorIdAndStateIn(LocalDateTime startTime, LocalDateTime endTime, String doctorId, int[] s);

    List<ConsultationInfoOrder> findAllByStateIn(Integer[] states);

}
